package com.dd.dc.base.config.redis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

/**
 * 描述: 定义监听器 RedisKeyExpirationListener，实现KeyExpirationEventMessageListener 接口，
 * 查看源码发现，该接口监听所有 db 的过期事件 keyevent@*:expired"
 *
 * @author: yanglin
 * @Date: 2021-02-22-9:14
 * @Version: 1.0
 */
@Slf4j
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    public RedisKeyExpirationListener(RedisMessageListenerContainer container) {
        super(container);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        // 获取到失效的 key，进行取消订单业务处理
        String expiredKey = message.toString();
        log.info("失效的 key>>>>>>>>>>>>>>>{}", expiredKey);
    }
}
